package com.hgyc.mom.mes.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.mes.entity.MesTeam;
import com.hgyc.mom.mes.vo.MesTeamParam;
import com.hgyc.mom.mes.service.MesTeamService;
import com.hgyc.mom.mes.vo.MesTeamVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 班组Controller
 *
 * @author fwj
 * @since 2025-07-01
 */
@Tag(name = "班组控制器", description = "班组控制器")
@RestController
@RequestMapping("/mes/team")
public class MesTeamController {
    @Autowired
    private MesTeamService mesTeamService;

    @Operation(summary = "查询班组列表", description = "查询班组列表")
    @PostMapping("/pageList")
    public Result<IPage<MesTeam>> pageList(@Parameter(description = "查询参数") @RequestBody MesTeamParam mesTeamParam) {
        Page<MesTeam> page = new Page<>(mesTeamParam.getPage(), mesTeamParam.getPageSize());
        MesTeam mesTeam = new MesTeam();
        BeanUtils.copyProperties(mesTeamParam, mesTeam);
        QueryWrapper<MesTeam> wrapper = QueryWrapperUtil.build(mesTeam);
        IPage<MesTeam> list = mesTeamService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取班组详细信息", description = "获取班组详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "班组ID") @PathVariable("id") Long id) {
        return Result.success(mesTeamService.getById(id));
    }
    @Operation(summary = "新增班组", description = "新增班组")
    @PostMapping
    public Result<?> save(@Parameter(description = "班组") @RequestBody MesTeamVO mesTeamVO) {
        mesTeamService.saveTeam(mesTeamVO);
        return Result.success();
    }

    @Operation(summary = "修改班组", description = "修改班组")
    @PutMapping
    public Result<?> update(@Parameter(description = "班组") @RequestBody MesTeamVO mesTeamVO) {
        mesTeamService.updateTeam(mesTeamVO);
        return Result.success();
    }

    @Operation(summary = "删除班组", description = "删除班组")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(mesTeamService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<MesTeam> util = new ExcelUtil<>(MesTeam.class);
        util.importTemplateExcel(response, "班组");
    }

    @Operation(summary = "导出班组列表", description = "导出班组列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesTeamParam mesTeamParam) {
        MesTeam mesTeam = new MesTeam();
        BeanUtils.copyProperties(mesTeamParam, mesTeam);
        QueryWrapper<MesTeam> wrapper = QueryWrapperUtil.build(mesTeam);
        List<MesTeam> list = mesTeamService.list(wrapper);
        ExcelUtil<MesTeam> util = new ExcelUtil<>(MesTeam.class);
        util.exportExcel(response, list, "班组数据");
    }

    @Operation(summary = "导入班组列表", description = "导入班组列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MesTeam> util = new ExcelUtil<>(MesTeam.class);
        List<MesTeam> mesTeamList = util.importExcel(file.getInputStream());
        String message = mesTeamService.importData(mesTeamList, updateSupport);
        return Result.success(message);
    }
}